package cn.xn.hrms.domain.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.PersonalTrainDTO;
import cn.xn.hrms.api.dto.req.PersonalTrainReqDTO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.domain.service.PersonalTrainService;
import cn.xn.hrms.infrastructure.dao.PersonalTrainDao;

/**
 * @author ysz
 */
@Service
public class PersonalTrainServiceImpl implements PersonalTrainService {

    @Resource
    PersonalTrainDao personalTrainDao;
    @Resource
    PersonalService personalService;

    @Override
    public PageInfo<PersonalTrainDTO> findAll(int pageNum, int pageSize, Integer departmentId, int personalId, Date beginDate, Date endDate) {
        PageHelper.startPage(pageNum, pageSize);
        List<PersonalTrainDTO> personalTrainDTOS = personalTrainDao.selectAll(departmentId, personalId, beginDate, endDate);
        return new PageInfo<>(personalTrainDTOS);
    }

    @Override
    public List<PersonalTrainDTO> all() {
        return personalTrainDao.selectAll(null, 0, null, null);
    }

    @Override
    public void insert(PersonalTrainReqDTO personalTrainReqDTO) {
        PersonalTrainDTO personalTrainDTO = new PersonalTrainDTO();
        BeanUtils.copyProperties(personalTrainReqDTO, personalTrainDTO);
        personalTrainDao.insertSelective(personalTrainDTO);
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        personalTrainDao.updateIsDeletedByIdIn(1, idList);
    }

    @Override
    public void updateById(Integer id, PersonalTrainReqDTO personalTrainReqDTO) {
        PersonalTrainDTO personalTrainDTO = personalTrainDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(personalTrainDTO)) {
            throw new BaseException(ResultEnum.PERSONAL_TRAIN_NOT_EXIST);
        }
        BeanUtils.copyProperties(personalTrainReqDTO, personalTrainDTO);
        personalTrainDao.updateByPrimaryKeySelective(personalTrainDTO);
    }
}
